package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.config.UserContext;
import com.zzx.entity.TCompany;
import com.zzx.entity.TProductType;
import com.zzx.mapper.TCompanyMapper;
import com.zzx.service.TCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author CD
 * @description 针对表【t_company】的数据库操作Service实现
 * @createDate 2024-11-28 17:27:07
 */
@Service
public class TCompanyServiceImpl extends ServiceImpl<TCompanyMapper, TCompany>
        implements TCompanyService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TCompany param) {
        LambdaQueryWrapper<TCompany> wrapper = new LambdaQueryWrapper<>(TCompany.class);
        wrapper.eq(TCompany::getCompanyName, param.getCompanyName());
        if (ObjectUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TCompany::getCompanyId, param.getCompanyId());
            BeanUtil.copyProperties(this.getBaseMapper().selectById(param.getCompanyId()), param);
        }
        List<TCompany> tCompanies = this.getBaseMapper().selectList(wrapper);
        if (!tCompanies.isEmpty()) {
            throw new RuntimeException("名称重复");
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TCompany param) {
        this.removeById(param.getCompanyId());
    }

    @Override
    public List<TCompany> selectBy(TCompany param) {
        LambdaQueryWrapper<TCompany> wrapper = new LambdaQueryWrapper<>(TCompany.class);
        if (!ObjectUtils.isEmpty(param.getCompanyName())) {
            wrapper.like(TCompany::getCompanyName, param.getCompanyName());
        }
        if (!ObjectUtils.isEmpty(param.getCompanyCode())) {
            wrapper.like(TCompany::getCompanyCode, param.getCompanyCode());
        }
        if (!ObjectUtils.isEmpty(param.getCompanyId())) {
            wrapper.eq(TCompany::getCompanyId, param.getCompanyId());
        }
        return this.getBaseMapper().selectList(wrapper);
    }
    @Override
    public TCompany findById(String companyId) {
        return this.getBaseMapper().selectById(companyId);
    }
}




